package cn.kzgg.pet.service.impl;

import cn.kzgg.pet.domain.OrderPetAcquisition;
import cn.kzgg.pet.domain.Pet;
import cn.kzgg.pet.domain.SearchMasterMsg;
import cn.kzgg.pet.dto.OrderHandleDto;
import cn.kzgg.pet.mapper.OrderPetAcquisitionMapper;
import cn.kzgg.pet.mapper.PetMapper;
import cn.kzgg.pet.mapper.SearchMasterMsgMapper;
import cn.kzgg.pet.service.IOrderPetAcquisitionService;
import cn.kzgg.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author KZGG
 * @since 2022-06-29
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {


    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;


    /**
     * 订单处理：确认订单
     * @param dto
     */
    @Override
    public void confirm(OrderHandleDto dto) {
        //1.拿到对应的订单
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(dto.getId());
        //2.订单新信息的补充：状态、支付方式、价钱
        order.setState(1);
        order.setPaytype(dto.getPayType());
        order.setPrice(dto.getMoney());
        orderPetAcquisitionMapper.update(order);
        //3.修改寻主状态
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        msg.setState(2);
        searchMasterMsgMapper.update(msg);
        //4.修改宠物成本价格
        Pet pet = petMapper.loadById(order.getPetId());
        pet.setCostprice(dto.getMoney());
        petMapper.update(pet);



    }

    /**
     * 订单处理：取消订单
     * @param id
     */
    @Override
    public void cancel(Long id) {
        //1.根据订单id找到对应的订单
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        //2.将状态改为待处理
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);
        //3.将寻主shopId置空放进寻主池
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getShopId());
        msg.setShopId(null);
        searchMasterMsgMapper.update(msg);
        //4.删除宠物
        petMapper.deleteById(order.getPetId());
    }
}
